<template>
  <div class="cont" v-show="contShow">
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="cont_top"
      label-width="50px"
      @submit.prevent="handleQuery"
    >
      <el-form-item style="margin-right: 12px">
        <el-button
          link
          type="primary"
          @click="addbtn"
          class="btnhover"
          style="
            width: 90px;
            height: 30px;
            background-color: #ff9900;
            color: rgba(255, 255, 255, 1);
          "
          >新增</el-button
        >
        <el-button
          link
          type="primary"
          @click="batchDelete"
          class="btnhover"
          style="
            width: 90px;
            height: 30px;
            background-color: #ff9900;
            color: rgba(255, 255, 255, 1);
          "
          >批量删除</el-button
        >
      </el-form-item>
      <el-form-item label="标题" prop="topic" style="margin-right: 17px">
        <el-input
          v-model="queryParams.topic"
          placeholder=""
          @keyup.enter="handleQuery"
          style="width: 200px"
        />
      </el-form-item>
      <el-form-item style="margin-right: 0">
        <el-button @click="handleQuery" class="btn btnsearch">查询</el-button>
        <el-button @click="resetQuery" class="btn btnreset">重置</el-button>
      </el-form-item>
    </el-form>
    <div style="display: flex">
      <!-- 选项卡· -->
      <div
        style="
          min-width: 210px;
          max-width: 400px;
          margin-right: 18px;
          height: 100%;
          border: 1px solid rgba(228, 228, 228, 1);
          margin-bottom: 20px;
        "
      >
        <div class="tabtop">
          <div style="width: 110px; line-height: 49px; font-weight: 550">分类</div>
          <div
            ><el-icon
              title="添加"
              style="color: #cc352f; margin-right: 16px"
              @click="addClassifiy()"
              ><Plus /> </el-icon
          ></div>
        </div>
        <div
          class="tabcont"
          v-for="(item, index) in optiontype"
          :key="item.id"
          :class="{ selected: isActive === item.id }"
          @click="colorbtn(item.id, item)"
        >
          <div
            v-html="item.label.length > 7 ? item.label.slice(0, 7) + '...' : item.label"
            :title="item.label"
            style="width: 140px; line-height: 40px; text-indent: 16px"
          ></div>
          <div v-if="item.label !== '全部'" style="margin-right: 14px">
            <el-icon style="margin-right: 8px" title="编辑" @click="editaddClassifiy(item, index)">
              <Edit />
            </el-icon>
            <el-icon title="删除" @click="delphoto(item, index)">
              <Delete />
            </el-icon>
          </div>
          <div v-else>
            <div style="width: 35px; height: 14px; margin-right: 14px"></div>
          </div>
        </div>
      </div>
      <!-- 表格 -->
      <el-table
        v-loading="loading"
        element-loading-text="Loading..."
        :data="list"
        style="margin-bottom: 20px; min-height: 435px"
        :header-cell-style="{ background: '#F0F0F0' }"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" />
        <el-table-column align="center" label="标题" width="auto">
          <template #default="scope">
            <div class="namecont" :title="scope.row.topic">
              {{ scope.row.topic }}
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="分类" prop="articleType">
          <template #default="{ row }">
            {{ imageTextTypeRelate(row) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="阅读人数" prop="viewerCount" width="auto" />
        <el-table-column align="center" label="创建人" prop="creator" width="auto" />
        <el-table-column align="center" label="创建时间" prop="createTime" width="200">
          <template #default="scope">
            {{ formatDate(scope.row.createTime, 'YYYY-MM-DD HH:mm ') }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" fixed="right">
          <template #default="scope">
            <el-button
              link
              type="primary"
              class="btnhover"
              @click="detailbtn(scope.row)"
              style="color: #3c88f0"
              >详情</el-button
            >
            <el-button
              link
              type="primary"
              class="btnhover"
              @click="editbtn(scope.row)"
              style="color: #3c88f0"
              >编辑</el-button
            >
            <el-button
              link
              type="primary"
              class="btnhover"
              @click="delbtn(scope.row)"
              style="color: #3c88f0"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <el-pagination
      style="font-size: 14px"
      v-model:current-page="queryParams.pageNo"
      :hide-on-single-page="value"
      v-model:page-size="queryParams.pageSize"
      :page-sizes="pageSizes"
      layout="total,sizes,prev, pager, next,jumper"
      :total="total"
      @size-change="selectchange"
      @current-change="handleCurrentChange"
    />
  </div>
  <AddDialog
    v-show="addShow"
    :addShow="addShow"
    @getaddShow="getaddShow"
    :editData="editData"
    @geteditData="geteditData"
  />
  <EditDialog v-model:visible="editDialogVisible" @getedit="getedit" :editData="editData" />
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import AddDialog from './components/addDialog.vue'
import EditDialog from './components/editDialog.vue'
import { formatDate } from '@/utils/formatTime'
import globalApi from '@/api/spb/global'
import * as getArticleApi from '@/api/appManagement/articleManagement/index'
const message = useMessage()

const { push } = useRouter() // 路由
const selectList = ref([]) // 表格选中数据
const loading = ref(true) // 列表加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const value = ref(true)
const pageSizes = [10, 20, 50, 100]
const editDialogVisible = ref(false)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  topic: ''
})
const optiontype = ref([{ id: 0, label: '全部' }])
const isActive = ref(0)
const queryFormRef = ref() // 搜索的表单
const addShow = ref(false)
const contShow = ref(true)
const editData = ref({})
let treeData = ref([])

const activeVal = ref(0)
const getaddShow = (val) => {
  addShow.value = val
  contShow.value = !val
}
/** 获取分类列表 */
const getDictData = () => {
  const params = {
    pageNo: 1,
    pageSize: 100,
    dictType: 'countryside_app_article_type'
  }
  globalApi.getDictData(params).then((res) => {
    treeData.value = res.list
    optiontype.value = [{ id: 0, label: '全部' }]
    let data = reactive(res.list)
    data.forEach((item) => {
      optiontype.value.push(item)
    })
  })
}
/** 编辑分类 */
const editaddClassifiy = (item, index) => {
  editDialogVisible.value = true
  editData.value = {}
  editData.value.name = item.label
  editData.value.dialogtitle = '编辑分类'
  editData.value.editVal = item
}

/** 新增分类 */
const addClassifiy = () => {
  editDialogVisible.value = true
  editData.value = {}
  editData.value.dialogtitle = '新增分类'
  editData.value.treeData = treeData.value
  editData.value.pageNo = 1
}

const geteditData = (val) => {
  handleCurrentChange(val)
}
const getedit = (val) => {
  handleCurrentChange(val)
}
/** 条数变化 */
const selectchange = (e) => {
  queryParams.pageSize = e
  queryParams.pageNo = 1
  getList()
  getDictData()
}
/** 页码变化 */
const handleCurrentChange = (e) => {
  queryParams.pageNo = e
  getList()
  getDictData()
}

/** 删除分类 */
const delphoto = (item) => {
  ElMessageBox.confirm('此操作将永久删除该分类, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      const params = {
        id: item.id
      }
      globalApi.delDictData(params).then(() => {
        message.success('删除成功')
        getDictData()
      })
    })
    .catch(() => {
      ElMessage.info('已取消删除')
    })
}
/** 选项卡颜色 */
const colorbtn = (id, item) => {
  isActive.value = item.id
  activeVal.value = Number(item.value) || 0
  getList()
}
/** 查询任务列表 */
const getList = async () => {
  const params = {
    pageNo: queryParams.pageNo,
    pageSize: queryParams.pageSize,
    topic: queryParams.topic,
    articleType: activeVal.value
  }
  getArticleApi
    .getArticlePage(params)
    .then((res) => {
      let data = reactive(res.list)
      total.value = res.total
      list.value = data
      if (list.value.length === 0) {
        value.value = true
      } else {
        value.value = false
      }
      loading.value = false
    })
    .catch((error) => {
      loading.value = false
    })
}

/** 批量删除 */

const handleSelectionChange = (e) => {
  selectList.value = e
}
const batchDelete = () => {
  if (selectList.value.length == 0) {
    ElMessage({
      message: '至少选中一条数据！',
      type: 'warning'
    })
    return
  }

  ElMessageBox.confirm('此操作将永久删除选中文章, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      const idList = reactive([])
      selectList.value.forEach((item) => {
        idList.push(item.id)
      })
      getArticleApi.deleteArticle(idList).then((res) => {
        if (res === true) {
          ElMessage.success('删除成功！')
          getList()
        }
      })
    })
    .catch(() => {
      ElMessage.info('已取消删除')
    })
}

// 表格分类字段映射
const imageTextTypeRelate = (row) => {
  let content = ''
  treeData.value.forEach((t) => {
    if (t.value == row.articleType) {
      content = t.label
    }
  })
  return content
}

/** 删除 */
const delbtn = (row: any) => {
  ElMessageBox.confirm('此操作将永久删除该文章, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      getArticleApi.deleteArticle([row.id]).then((res) => {
        if (res === true) {
          ElMessage.success('删除成功！')
          getList()
        }
      })
    })
    .catch(() => {
      ElMessage.info('已取消删除')
    })
}
/** 新增 */
const addbtn = () => {
  addShow.value = true
  contShow.value = false
  editData.value = {}
  editData.value.dialogtitle = '新增文章'
  editData.value.isDisplay = '1'
  editData.value.pageNo = 1
  editData.value.treeData = treeData.value
}
/** 编辑 */
const editbtn = (row: any) => {
  addShow.value = true
  contShow.value = false
  editData.value = row
  editData.value.dialogtitle = '编辑文章'
  editData.value.pageNo = 1
  editData.value.treeData = treeData.value
}
/** 详情 */
const detailbtn = (row: any) => {
  push({
    name: 'articleDetail',
    query: {
      id: row.id
    }
  })
}
/** 查询按钮操作 */
const handleQuery = () => {
  queryParams.topic = queryParams.topic
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 初始化 **/
onMounted(() => {
  getList()
  getDictData()
})
</script>

<style scoped>
.cont {
  background-color: rgba(255, 255, 255, 1);
  color: rgba(16, 16, 16, 1);
  font-size: 14px;
  text-align: center;
  min-height: 557px;
  position: relative;
}

.cont_top {
  text-align: left;
  margin: 4px 0;
}

::v-deep .el-table .cell {
  display: flex;
  justify-content: center;
}

::v-deep .el-input__wrapper.is-focus {
  box-shadow: 0 0 0 1px #cc352f;
}

::v-deep .el-select__wrapper.is-focused {
  box-shadow: 0 0 0 1px #cc352f;
}

.btn {
  width: 80px;
  height: 30px;
  line-height: 20px;
  border-radius: 5px;
  font-size: 14px;
  text-align: center;
}

.btnreset {
  background-color: rgba(255, 255, 255, 1);
  border: 1px solid rgba(187, 187, 187, 1);
  color: rgba(142, 142, 142, 1);
}

.btnsearch {
  background-color: #cc352f;
  color: rgba(255, 255, 255, 1);
  border: 1px solid #cc352f;
}

.btn:hover {
  opacity: 0.8;
}

.btnhover:hover {
  opacity: 0.8;
}

.el-pagination {
  justify-content: center;
}

.namecont {
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: center;
}

::v-deep .el-form-item__label {
  padding: 0 17px 0 0;
}

.tabtop {
  width: 100%;
  height: 49px;
  border-bottom: 1px solid rgba(228, 228, 228, 1);
  text-indent: 14px;
  text-align: left;
  line-height: 52px;
  /* margin-bottom: 10px; */
  display: flex;
  justify-content: space-between;
}

.tabcont {
  width: calc(100% -3px);
  height: 40px;
  text-align: left;
  margin-bottom: 5px;
  color: #8e8e8e;
  cursor: pointer;
  line-height: 40px;
  display: flex;
  justify-content: space-between;
  border-right: 3px solid #fff;
}

.selected {
  border-right: 3px solid #cc352f;
  color: #cc352f;
  height: 40px;
  line-height: 40px;
  background-color: rgba(188, 79, 79, 0.13);
}

@media screen and (min-width: 1700px) {
  .heightclass {
    display: flex;
    justify-content: space-between;
  }
}

@media (min-width: 832px) and (max-width: 1000px) {
  .heightclass {
    display: flex;
    justify-content: space-between;
  }
}

@media (min-width: 670px) and (max-width: 765px) {
  .heightclass {
    display: flex;
    justify-content: space-between;
  }
}
</style>
